package com.woniu.car.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.domain.TBackstageAccount;
import com.woniu.car.domain.TRole;
import com.woniu.car.domain.TShopAccount;
import com.woniu.car.domain.TUrl;
import com.woniu.car.dto.AllBackstageDto;
import com.woniu.car.dto.PageDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TBackstageAccountMapper;
import com.woniu.car.mapper.TRoleMapper;
import com.woniu.car.mapper.TShopAccountMapper;
import com.woniu.car.mapper.TUrlMapper;
import com.woniu.car.param.PageParam;
import com.woniu.car.param.RoleAuthorizeParam;
import com.woniu.car.param.UrlRolesJson;
import com.woniu.car.service.TShopAccountService;
import com.woniu.car.service.TUrlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TUrlServiceImpl
 * @Description
 * @Author 王维鑫
 * @Version
 **/
@Service
public class TUrlServiceImpl extends ServiceImpl<TUrlMapper, TUrl> implements TUrlService {
    @Resource
    private TUrlMapper tUrlMapper;
    @Resource
    private TBackstageAccountMapper tBackstageAccountMapper;
    @Resource
    private TRoleMapper tRoleMapper;
    /**
     *@Author 王维鑫
     *@Description 查找某个功能
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TUrl queryUrl(TUrl tUrl) {
        QueryWrapper<TUrl> wrapper = new QueryWrapper<>();
        if(null!=tUrl.getUrlId()){
            wrapper.eq("url_id",tUrl.getUrlId());
        }
        if(null!=tUrl.getUrl()){
            wrapper.eq("url",tUrl.getUrl());
        }
        if(null!=tUrl.getUrlName()){
            wrapper.eq("url_name",tUrl.getUrlName());
        }
        TUrl tUrl1 = tUrlMapper.selectOne(wrapper);
        return tUrl1;
    }
    /**
     *@Author 王维鑫
     *@Description 展示所有功能
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TUrl> showAllUrls() {
        QueryWrapper<TUrl> wrapper = new QueryWrapper<>();
        List<TUrl> tUrls = tUrlMapper.selectList(wrapper);
        return tUrls;
    }
    /**
     *@Author 王维鑫
     *@Description 给角色授权
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void roleAuthorize(RoleAuthorizeParam param) throws ParamException {
        //查询员工ID是否存在
        Integer id = param.getBackstageId();
        TBackstageAccount tBackstageAccount=null;
        tBackstageAccount = tBackstageAccountMapper.selectById(id);
        if(null==tBackstageAccount){
            throw new AccountException("该员工不存在");
        }
        //查询功能是否存在
        Integer urlId = param.getUrlId();
        TUrl tUrll=null;
        tUrll = tUrlMapper.selectById(urlId);
        if(null==tUrll){
            throw new AccountException("该功能不存在");
        }
        //查询角色是否存在
        Integer roleId1 = param.getRoleId();
        TRole tRole=null;
        tRole = tRoleMapper.selectById(roleId1);
        if(null==tRole){
            throw new AccountException("该角色不存在");
        }
        TUrl tUrl = new TUrl();
        tUrl.setUrlId(urlId);
        //根据功能ID找到对应功能信息
        TUrl tUrl1 = this.queryUrl(tUrl);
        String roleIds = tUrl1.getRoleIds();
        UrlRolesJson urlRolesJson = new UrlRolesJson();
        //如果功能对应角色为空直接新增
        if(null==roleIds||roleIds.equals("")){
            ArrayList<UrlRolesJson> urlRolesJsons = new ArrayList<>();
            urlRolesJson.setRoleId(param.getRoleId());
            urlRolesJson.setBackstageId(id);
            urlRolesJsons.add(urlRolesJson);
            String s = JSON.toJSONString(urlRolesJsons);
            tUrl1.setRoleIds(s);
            tUrlMapper.updateById(tUrl1);
        }else {
            //将对应角色转成集合再添加后再更新
            List<UrlRolesJson> urlRolesJsons = JSONObject.parseArray(roleIds, UrlRolesJson.class);
            //查看集合里面有没有当前角色
            for (UrlRolesJson rolesJson : urlRolesJsons) {
                if(rolesJson.getRoleId().equals(roleId1)){
                    throw new ParamException("该角色已经拥有该功能，请勿重复设置");
                }
            }
            urlRolesJson.setRoleId(roleId1);
            urlRolesJson.setBackstageId(id);
            urlRolesJsons.add(urlRolesJson);
            String s = JSON.toJSONString(urlRolesJsons);
            tUrl1.setRoleIds(s);
            tUrlMapper.updateById(tUrl1);
        }
    }

    @Override
    @Transactional
    public List<AllBackstageDto> queryAllBackStage() {
        QueryWrapper<TBackstageAccount> wrapper = new QueryWrapper<>();
        List<TBackstageAccount> tBackstageAccounts = tBackstageAccountMapper.selectList(wrapper);
        List<AllBackstageDto> allBackstageDtos = BeanCopyUtils.copyList(tBackstageAccounts, AllBackstageDto::new);
        return allBackstageDtos;
    }

    @Override
    @Transactional
    public PageDto showAllUrlss(PageParam param) {
        Page<TUrl> tUrlPage = new Page<>(param.getPageNum(),param.getPageSize());
        QueryWrapper<TUrl> wrapper = new QueryWrapper<>();
        Page<TUrl> tUrlPage1 = tUrlMapper.selectPage(tUrlPage, wrapper);
        PageDto objectPageDto = new PageDto();
        objectPageDto.setCurrent(tUrlPage1.getCurrent());
        objectPageDto.setSize(tUrlPage1.getSize());
        objectPageDto.setTotal(tUrlPage1.getTotal());
        objectPageDto.setList(tUrlPage1.getRecords());
        return objectPageDto;
    }
}

